package com.dongdongshop.service.impl;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.model.TbSeckillOrder;
import com.dongdongshop.uitl.IdWorker;
import com.dongdongshop.vo.SeckillGoodsVO;
import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.model.TbSeckillGoods;
import com.dongdongshop.model.TbSeckillGoodsExample;
import com.dongdongshop.service.SeckillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SeckillServiceImpl implements SeckillService {
    @Autowired
    private TbSeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TbSeckillOrderMapper seckillOrderMapper;

    @Override
    public void cachePreHot() {
        //定义一个当前时间
        Date now = new Date();
        TbSeckillGoodsExample example = new TbSeckillGoodsExample();
        TbSeckillGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andStartTimeLessThanOrEqualTo(now);  //开始时间小于等于当前时间
        criteria.andEndTimeGreaterThanOrEqualTo(now);   //结束时间大于等于当前时间
        List<TbSeckillGoods> tbSeckillGoods = seckillGoodsMapper.selectByExample(example);
        tbSeckillGoods.stream().map(s->{
            SeckillGoodsVO vo = new SeckillGoodsVO();
            BeanUtils.copyProperties(s,vo);
            return vo;
        }).forEach(v->{
            //存入redis
            redisTemplate.boundHashOps("seckillList").put(v.getId(),v);
        });
    }

    @Override
    public List<SeckillGoodsVO> seckillList() {
        return (List<SeckillGoodsVO>)redisTemplate.boundHashOps("seckillList").values();
    }

    @Override
    @SentinelResource("seckillGoods")
    public SeckillGoodsVO seckillGoods(Long id) {
        return (SeckillGoodsVO)redisTemplate.boundHashOps("seckillList").get(id);
    }

    @Override
    public void submitOrder(String userId, Long id) {
        //根据商品的id从redis中取出要秒杀的商品
        SeckillGoodsVO goodsVO = seckillGoods(id);
        if (goodsVO==null){
            throw new RuntimeException("秒杀商品不存在");
        }
        //时间:
        if (goodsVO.getEndTime().getTime()-new Date().getTime()<=0){
            throw new RuntimeException("秒杀已结束");
        }
        //判断商品的库存是否还有
        if (goodsVO.getStockCount()<=0){
            throw new RuntimeException("商品已售罄");
        }
        //扣减库存,生成订单
        goodsVO.setStockCount(goodsVO.getStockCount()-1);
        //再放入redis中
        redisTemplate.boundHashOps("seckillList").put(goodsVO.getId(),goodsVO);

        //当减到最后一个商品时,我们同步数据库
        if (goodsVO.getStockCount()==0){
            TbSeckillGoods seckillGoods = new TbSeckillGoods();
            BeanUtils.copyProperties(goodsVO,seckillGoods);
            seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
        }


        //创建订单
        TbSeckillOrder seckillOrder = new TbSeckillOrder();
        long orderId = idWorker.nextId();
        seckillOrder.setId(orderId);
        seckillOrder.setCreateTime(new Date());
        seckillOrder.setMoney(goodsVO.getCostPrice());  //支付的钱
        seckillOrder.setSeckillId(id);   //秒杀商品的id
        seckillOrder.setSellerId(goodsVO.getSellerId());  //商家id
        seckillOrder.setStatus("0");  //待支付
        seckillOrder.setUserId(userId);   //卖家id
        seckillOrderMapper.insertSelective(seckillOrder);


    }
}
